package UI.bll;

import BLL.establishDatabase;

import java.awt.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import static java.lang.String.valueOf;

/**
 * @ClassName connect
 * @Description: TODO
 * @Author YELIUHUISHI
 * @Date 2020/2/28
 * @Version V1.0
 **/


public class connect extends javax.swing.JFrame {

  static  boolean flag=false;

    /**
     * Creates new form connect
     */
    public connect() {
        initComponents();
        init();
    }

    private void init() {
        this.setTitle("连接");
        this.setResizable(false);
        uiutlLogin uiutlLogin = new uiutlLogin();
        uiutlLogin.setFrameConter(this);
        uiutlLogin.setFrameTmage(this,"src"+File.separator+ "UI" +File.separator+"icon"+File.separator+"connect.png");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        ipAddrs = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        ipPassword = new javax.swing.JPasswordField();
        savePassword = new javax.swing.JRadioButton();
        test = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        start = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("主机民或IP地址:");

        jLabel2.setText("密码:");

        savePassword.setText("保持密码");

        test.setText("测试连接");
        test.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testActionPerformed(evt);
            }
        });
        jLabel3.setFont(new java.awt.Font("宋体", 0, 19)); // NOI18N
        start.setText("连接");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(22, 22, 22)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(jLabel1)
                                                        .addComponent(jLabel2)))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(test)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                                                .addComponent(start))
                                        .addComponent(savePassword, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(ipPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                                        .addComponent(ipAddrs, javax.swing.GroupLayout.Alignment.LEADING))
                                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(ipAddrs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(23, 23, 23)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(ipPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(savePassword)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(start)
                                                .addGap(22, 22, 22))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(test, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                .addGap(0, 0, Short.MAX_VALUE))))
        );

        pack();
    }// </editor-fold>

    //测试连接
    private void testActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            joinResults();
            if (flag==false){
                return;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //连接
    private void startActionPerformed(java.awt.event.ActionEvent evt) {

        try {
            if(flag==false){
                joinResults();
                if (flag==false){
                    return;
                }
                new establishDatabase().checkDatabase();
                new Login().setVisible(true);
                this.dispose();
            }else{
                new Login().setVisible(true);
                this.dispose();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return;
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(()->{
            new connect().setVisible(true);
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JTextField ipAddrs;
    private javax.swing.JPasswordField ipPassword;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JRadioButton savePassword;
    private javax.swing.JButton start;
    private javax.swing.JButton test;
    // End of variables declaration


    private void joinResults() throws ClassNotFoundException {
        /**
         *  1\获取IP地址与密码
         *  2\判断密码是否需要保存
         *
         */
        String ipAddr = this.ipAddrs.getText().trim();//获取Ip
        String ipPassord= valueOf(this.ipPassword.getPassword());;//获取密码


        try {
            String DB_URL="jdbc:mysql://"+ipAddr+":3306";
            String mysqlUser="root";
            Connection connection=null;
            connection=DriverManager.getConnection(DB_URL,mysqlUser,ipPassord);
            if (connection==null){
            //连接失败
                jLabel3.setForeground(Color.red);
                jLabel3.setText("×");
            }else{
            //连接成功
                jLabel3.setForeground(Color.GREEN);
                jLabel3.setText("√");
                //判断文件是否需要保存
                if(savePassword.isSelected()){
                    jLabel3.setText("√√");
                }
                flag=true;
            }
            String path="src/configuration/sequential_query.properties";
            Properties properties = new Properties();
            //Enumeration<?> e=properties.propertyNames();
            //这里不能使用Properties的store()方法,因为冒号会被转义
            FileOutputStream fileOutputStream = new FileOutputStream(path,false);
            String mysql="mysqlLogin="+DB_URL+"/sequential_query?useUnicode=true&characterEncoding=utf-8&createDatabaseIfNotExist=true";
            DB_URL="mysqlURL="+DB_URL+"\n";
            mysqlUser="mysqlUser="+mysqlUser+"\n";
            ipPassord="mysqlPwd="+ipPassord+"\n";
            fileOutputStream.write(DB_URL.getBytes());
            fileOutputStream.write(mysqlUser.getBytes());
            fileOutputStream.write(ipPassord.getBytes());
            fileOutputStream.write(mysql.getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (SQLException | FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
